﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Clipping.Infrastructure.Models;
using System.Windows.Input;
namespace ClippingMaster
{
    class KeywordSettingViewModel : GalaSoft.MvvmLight.ViewModelBase
    {

        public event Action OnSaved;
        public event Action OnCanceled;

        private OpMode mode;
        public KeywordSettingViewModel(KeywordCategory category)
        {
            initCommands();
            Guid categoryId = Guid.NewGuid();
            if (category != null)
                categoryId = category.Id;

            KeywordInfo = new KeywordInfo() { Id = Guid.NewGuid(), CategoryId = categoryId };
            this.mode = OpMode.Add;
            
        }
        public KeywordSettingViewModel(KeywordInfo keyword)
        {
            initCommands();
            KeywordInfo = keyword;
            this.Keyword = keyword.Keyword;
            this.SelectedSearchPeriod = keyword.SearchPeriod;
            this.SelectedSearchType = keyword.SearchType;
            this.mode = OpMode.Modify;
         
        }

        private void initCommands()

        {

            this.SaveCommand = new GalaSoft.MvvmLight.Command.RelayCommand(save, canSave);
            this.CancelCommand = new GalaSoft.MvvmLight.Command.RelayCommand(cancel);
        
        }

        private void save()
        {
            this.KeywordInfo.Keyword = this.Keyword;
            this.KeywordInfo.SearchPeriod = this.SelectedSearchPeriod;
            this.KeywordInfo.SearchType = this.SelectedSearchType;
            KeywordsRepository rep = new KeywordsRepository();

            if (this.mode == OpMode.Add)
            {
                rep.AddKeyword(this.KeywordInfo);
            }
            else
            {
                rep.UpdateKeyword(this.KeywordInfo);
            }
            var handler = this.OnSaved;
            if (handler != null)
                handler();
        }

        private bool canSave()
        {
            if (string.IsNullOrEmpty(Keyword))
                return false;
            else
                return true;
        }

        private void cancel()
        {
            var handler = this.OnCanceled;
            if (handler != null)
                handler();
        }

        #region 属性
        private string _keyword;
        public string Keyword
        {
            get { return _keyword; }
            set
            {
                if (value == this._keyword)
                    return;
                _keyword = value;
                RaisePropertyChanged(() => this.Keyword); 
                (SaveCommand as GalaSoft.MvvmLight.Command.RelayCommand).RaiseCanExecuteChanged();
            }
        }

        private SearchType _selectedSearchType= SearchType.SearchInContent;
        public SearchType SelectedSearchType
        {
            get { return _selectedSearchType; }
            set
            {
                if (value == this._selectedSearchType)
                    return;
                _selectedSearchType = value;
                RaisePropertyChanged(() => this.SelectedSearchType);
            }
        }

        private List<SearchPeriod> _searchPeriods = Clipping.Utils.EnumHelper.GetEnumValues<SearchPeriod>();
        public List<SearchPeriod> AllSearchPeriods
        {
            get { return _searchPeriods; }
        }

        private SearchPeriod _selectedSearchPeriod;
        public SearchPeriod SelectedSearchPeriod
        {
            get { return _selectedSearchPeriod; }
            set 
            {
                if (value == this._selectedSearchPeriod)
                    return;
                _selectedSearchPeriod = value;
                RaisePropertyChanged(() => this.SelectedSearchPeriod);
            }
        }

        public KeywordInfo KeywordInfo { get; private set; }
        #endregion

        public ICommand SaveCommand { get; private set; }
        public ICommand CancelCommand { get; private set; }

        enum OpMode
        {
            Add,
            Modify
        }
    }
}
